/**
 * 日期格式化
 * @param {Date} date 日期对象
 * @param {String} format 格式字符串
 * @return {String} 格式化后的日期字符串
 */
const formatDate = (date, format = 'YYYY-MM-DD') => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();

  return format
    .replace('YYYY', year)
    .replace('MM', month.toString().padStart(2, '0'))
    .replace('DD', day.toString().padStart(2, '0'))
    .replace('HH', hour.toString().padStart(2, '0'))
    .replace('mm', minute.toString().padStart(2, '0'))
    .replace('ss', second.toString().padStart(2, '0'));
};

/**
 * 计算BMI指数
 * @param {Number} weight 体重(kg)
 * @param {Number} height 身高(cm)
 * @return {Number} BMI指数
 */
const calculateBMI = (weight, height) => {
  if (!weight || !height) return 0;
  // 身高转换为米
  const heightInMeter = height / 100;
  // BMI = 体重(kg) / 身高(m)^2
  return (weight / (heightInMeter * heightInMeter)).toFixed(1);
};

/**
 * 根据BMI指数获取健康状态
 * @param {Number} bmi BMI指数
 * @return {Object} 健康状态对象
 */
const getBMIStatus = (bmi) => {
  let status = '';
  let color = '';
  
  if (bmi < 18.5) {
    status = '偏瘦';
    color = '#2196f3'; // 蓝色
  } else if (bmi >= 18.5 && bmi < 24) {
    status = '正常';
    color = '#4CAF50'; // 绿色
  } else if (bmi >= 24 && bmi < 28) {
    status = '偏胖';
    color = '#ff9800'; // 橙色
  } else if (bmi >= 28) {
    status = '肥胖';
    color = '#f44336'; // 红色
  }
  
  return { status, color };
};

/**
 * 获取今天和未来7天的日期数组
 * @return {Array} 日期数组
 */
const getWeekDates = () => {
  const dates = [];
  const today = new Date();
  
  for (let i = 0; i < 7; i++) {
    const date = new Date(today);
    date.setDate(date.getDate() + i);
    dates.push({
      date: formatDate(date),
      day: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][date.getDay()]
    });
  }
  
  return dates;
};

/**
 * 获取指定月份的所有日期
 * @param {Number} year 年份
 * @param {Number} month 月份(1-12)
 * @return {Array} 日期数组
 */
const getMonthDates = (year, month) => {
  const dates = [];
  const firstDay = new Date(year, month - 1, 1);
  const lastDay = new Date(year, month, 0);
  const daysInMonth = lastDay.getDate();
  
  // 获取本月第一天是星期几 (0-6)
  const firstDayWeek = firstDay.getDay();
  
  // 补充上个月的最后几天
  for (let i = 0; i < firstDayWeek; i++) {
    const prevDate = new Date(year, month - 1, 0 - i);
    dates.unshift({
      date: formatDate(prevDate),
      day: prevDate.getDate(),
      currentMonth: false
    });
  }
  
  // 添加本月的日期
  for (let i = 1; i <= daysInMonth; i++) {
    const currentDate = new Date(year, month - 1, i);
    dates.push({
      date: formatDate(currentDate),
      day: i,
      currentMonth: true
    });
  }
  
  // 补充下个月的前几天，使总数为42（6周）
  const remainingDays = 42 - dates.length;
  for (let i = 1; i <= remainingDays; i++) {
    const nextDate = new Date(year, month, i);
    dates.push({
      date: formatDate(nextDate),
      day: i,
      currentMonth: false
    });
  }
  
  return dates;
};

module.exports = {
  formatDate,
  calculateBMI,
  getBMIStatus,
  getWeekDates,
  getMonthDates
}; 